package 二叉树;

import java.util.*;

public class LC_102二叉树的层次遍历 {

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }

 public List<List<Integer>> levelOrder(TreeNode root) {
     Queue<TreeNode> queue = new LinkedList<>();
     List<List<Integer>> result = new ArrayList<>();
     if (root != null)
        queue.add(root);
     while (!queue.isEmpty()) {
        int size = queue.size();
        List<Integer> vec = new ArrayList<>();
        for(int i=0;i<size;i++){
            TreeNode node = queue.poll();
            vec.add(node.val);
            if (node.left != null)
               queue.add(node.left);
            if (node.right != null)
               queue.add(node.right);
        }
        result.add(vec);
     }
  return result;
 }
}
